package com.example.moban.view.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.AppCompatTextView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.example.moban.R;


import butterknife.BindView;

/**
 * 取消确认弹框
 *
 * @author yql
 * @date: 2018/10/18.
 */
public class VersionUpdateDialog extends SimpleDialogFragment implements View.OnClickListener{


    @BindView(R.id.dialog_content_text)
    AppCompatTextView mDialogContent;
    @BindView(R.id.dialog_cancel_btn)
    AppCompatTextView mDialogCancel;
    @BindView(R.id.dialog_confirm_btn)
    AppCompatTextView mDialogCertain;
    @BindView( R.id.dialog_new_version_code_text )
    AppCompatTextView newVersionCodeText;

    private String newVersionCode;
    private String textContent;
    private String textCancel;
    private String textConfirm;
    private boolean isForce;
    private boolean isKillApp;

    private OnDialogListener mListener;

    @Override
    public void setContentView(@NonNull LayoutInflater inflater, FrameLayout contentView, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_version_update_layout, contentView, false);
        contentView.addView(view);
    }

    @Override
    public void initView(View view) {
        newVersionCodeText.setText( newVersionCode );
        mDialogContent.setText(textContent);
        if( isForce ){
            mDialogCancel.setVisibility( View.GONE );
        }else{
            mDialogCancel.setVisibility( View.VISIBLE );
        }
        mDialogCancel.setText(textCancel);
        mDialogCertain.setText(textConfirm);
        view.findViewById(R.id.dialog_cancel_btn).setOnClickListener( this );
        view.findViewById(R.id.dialog_confirm_btn).setOnClickListener( this );
        mDialogContent.setVisibility(TextUtils.isEmpty(textContent) ? View.GONE : View.VISIBLE);
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if( mListener != null ){
                        mListener.onBack();
                    }
                }
                return false;
            }
        });
    }

    @Override
    public void initData() {

    }

    /**
     * 显示弹框
     *
     * @param manager
     * @param content
     * @param cancel
     * @param confirm
     */
    public void showDialog(FragmentManager manager, String newVersionCode , String content,
                           String cancel, String confirm, boolean isForce , OnDialogListener listener) {
        this.newVersionCode = newVersionCode;
        this.textContent = content;
        this.textCancel = cancel;
        this.textConfirm = confirm;
        this.isForce = isForce;
        this.mListener = listener;
        show(manager, TAG);
    }

    @Override
    public void onClick(View v) {
        switch ( v.getId() ){
            case R.id.dialog_confirm_btn:
                if( mListener != null ){
                    mListener.confirm();
                }
                break;
            case R.id.dialog_cancel_btn:
                if( mListener != null ){
                    mListener.cancel();
                }
                break;
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        if( mListener != null ){
            mListener.dismiss();
        }
    }

    public void setKillAppOnBack(boolean killApp) {
        isKillApp = killApp;
    }

    public interface OnDialogListener{
         void confirm();
         void cancel();
         void onBack();
         void dismiss();
    }
}
